
package com.eyedog.video.search;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.eyedog.library.base.BaseFragment;
import com.eyedog.uilibrary.widgets.MyGridLayoutManager;
import com.eyedog.video.R;
import com.eyedog.video.entity.ADVEntity;
import com.eyedog.video.entity.CategoryEntity;
import com.eyedog.video.entity.HotTagEntity;
import com.eyedog.video.videolist.VideoListActivity;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

public class SearchFragment extends BaseFragment
        implements constract.ISearchView, View.OnClickListener {

    TagFlowLayout tagFlowLayout;

    ImageView advHeader, advTail;

    RecyclerView category;

    CategoryAdapter categoryAdapter;

    SearchPresenter presenter;

    EditText etSearch;

    TextView tvSearch;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.layout_search, container, false);
        return layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable
    final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tagFlowLayout = (TagFlowLayout) view.findViewById(R.id.id_flowlayout);
        tagFlowLayout.setOnTagClickListener(tagClickListener);
        advHeader = (ImageView) view.findViewById(R.id.iv_search_headeradv);
        advHeader.setOnClickListener(this);
        advTail = (ImageView) view.findViewById(R.id.iv_search_tailadv);
        advTail.setOnClickListener(this);
        category = (RecyclerView) view.findViewById(R.id.rv_category);
        category.setLayoutManager(new MyGridLayoutManager(getActivity(), 2));
        categoryAdapter = new CategoryAdapter(getActivity());
        category.setAdapter(categoryAdapter);
        etSearch = (EditText) view.findViewById(R.id.et_search);
        etSearch.addTextChangedListener(textWatcher);
        tvSearch = (TextView) view.findViewById(R.id.tv_search);
        tvSearch.setOnClickListener(this);

        presenter = new SearchPresenter(this, new SearchModel());
        presenter.displaySearch();
    }

    @Override
    public void showSearchHeaderAdv(final ADVEntity advEntity) {
        if (advEntity != null && !TextUtils.isEmpty(advEntity.img)) {
            advHeader.post(new Runnable() {
                @Override
                public void run() {
                    advHeader.setTag(R.id.tag_cache, advEntity);
                    advHeader.setVisibility(View.VISIBLE);
                    Glide.with(getActivity()).load(advEntity.img)
                            .placeholder(new ColorDrawable(Color.BLACK)).into(advHeader);
                }
            });
        }
    }

    @Override
    public void showSearchCategorys(final List<CategoryEntity> categoryEntities) {
        category.post(new Runnable() {
            @Override
            public void run() {
                category.setVisibility(View.VISIBLE);
                categoryAdapter.cleanData();
                categoryAdapter.add(categoryEntities);
            }
        });
    }

    @Override
    public void showSearchTailAdv(final ADVEntity advEntity) {
        if (advEntity != null && !TextUtils.isEmpty(advEntity.img)) {
            advTail.post(new Runnable() {
                @Override
                public void run() {
                    advTail.setTag(R.id.tag_cache, advEntity);
                    advTail.setVisibility(View.VISIBLE);
                    Glide.with(getActivity()).load(advEntity.img)
                            .placeholder(new ColorDrawable(Color.BLACK)).into(advTail);
                }
            });
        }
    }

    @Override
    public void showHotTags(final List<HotTagEntity> hotTagEntities) {
        if (hotTagEntities != null && hotTagEntities.size() > 0)
            tagFlowLayout.post(new Runnable() {
                @Override
                public void run() {
                    tagFlowLayout.setAdapter(new TagAdapter<HotTagEntity>(hotTagEntities) {
                        @Override
                        public View getView(FlowLayout parent, int position,
                                HotTagEntity hotTagEntity) {
                            TextView tv = (TextView) LayoutInflater.from(getActivity())
                                    .inflate(R.layout.item_hottag, parent, false);
                            tv.setText(hotTagEntity.keyword);
                            tv.setTag(hotTagEntity);
                            return tv;
                        }
                    });
                }
            });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search_headeradv:
            case R.id.iv_search_tailadv: {
                Object tag = view.getTag(R.id.tag_cache);
                if (tag != null && tag instanceof ADVEntity) {
                    ADVEntity advEntity = ((ADVEntity) tag);
                    if (advEntity != null && !TextUtils.isEmpty(advEntity.url)) {
                        Uri uri = Uri.parse(advEntity.url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                    }
                }
            }
                break;
            case R.id.tv_search: {
                if (etSearch.getText() != null) {
                    String keywords = etSearch.getText().toString().trim();
                    Bundle data = VideoListActivity.createVideoListBundle(keywords, keywords);
                    Intent intent = new Intent(getActivity(), VideoListActivity.class);
                    intent.putExtras(data);
                    startActivity(intent);
                }
            }
                break;
        }
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable != null && editable.length() > 0) {
                tvSearch.setEnabled(true);
            } else {
                tvSearch.setEnabled(false);
            }
        }
    };

    TagFlowLayout.OnTagClickListener tagClickListener = new TagFlowLayout.OnTagClickListener() {
        @Override
        public boolean onTagClick(View view, int position, FlowLayout parent) {
            View tagView = view.findViewById(R.id.tv_hottag);
            if (tagView != null) {
                Object tag = tagView.getTag();
                if (tag != null && tag instanceof HotTagEntity) {
                    String keywords = ((HotTagEntity) tag).keyword;
                    Bundle data = VideoListActivity.createVideoListBundle(keywords, keywords);
                    Intent intent = new Intent(getActivity(), VideoListActivity.class);
                    intent.putExtras(data);
                    startActivity(intent);
                }
            }
            return true;
        }
    };
}
